<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" import="java.util.*,java.text.SimpleDateFormat,com.brother.Constants,
com.brother.model.*,com.ibatis.common.util.PaginatedList,com.brother.util.Distance
,com.brother.util.DateFormatter,com.brother.model.AlarmType,com.brother.util.CountryLocation, com.brother.service.*"%><%
com.brother.model.Customer user = (com.brother.model.Customer)session.getAttribute(com.brother.Constants.LOGIN_SESSTION_NAME);
if(user == null){
	return;
}
java.text.DecimalFormat nf =new java.text.DecimalFormat("0.00");
String trackerid = request.getParameter("trackerid");
String from = request.getParameter("from");
String to = request.getParameter("to");
String interval = request.getParameter("interval");
  
System.out.println("trackerid=" + trackerid + ", from=" + from+", to="+to+",interval=" + interval);

String dateFormat = (String)session.getAttribute(Constants.DATE_FORMAT_SESSION_NAME);
SimpleDateFormat formatEn = new SimpleDateFormat("dd/MM/yyyy"); // English date format
SimpleDateFormat formatPT = new SimpleDateFormat("dd/MM/yyyy"); // Portul date format
SimpleDateFormat sessionFormat = new SimpleDateFormat(dateFormat);

SimpleDateFormat formatCN = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 
SimpleDateFormat DateFormatCN = new SimpleDateFormat("yyyy-MM-dd"); //

String extDateFormat = (String)session.getAttribute(Constants.EXT_DATE_FORMAT_SESSION_NAME);

DateFormatter localFormat = new DateFormatter(); // 输出时间
System.out.println("user time zone=" + user.getTimeZoneOffSet());
localFormat.setConfig(user.getTimeZoneOffSet(), dateFormat);

if(!DateFormatter.isChineaseFormat(from)){ // 非中文格式的日期
	from = formatCN.format(sessionFormat.parse(from));
	to = formatCN.format(sessionFormat.parse(to));
}
// 换成东八区时间
from = DateFormatter.toEastEight(formatCN.parse(from), user.getTimeZoneOffSet(), "yyyy-MM-dd HH:mm:ss");
Date toDateCN = DateFormatCN.parse(to);
toDateCN.setHours(toDateCN.getHours() + 24);// 添加二十四小时
to = DateFormatter.toEastEight(toDateCN, user.getTimeZoneOffSet(), "yyyy-MM-dd HH:mm:ss");
System.out.println("from="+from+",to="+to+" east 8 time zone");

ReportService rsv = new ReportService();
List<Report> result = rsv.findAllByRange(from, to, trackerid);

StringBuilder sb = new StringBuilder();
StringBuilder label = new StringBuilder();
sb.append("[");
label.append("[");
int timeInterval = 1; // 1天递增
Date fromDate = formatCN.parse(from);
Date toDate = formatCN.parse(to);

long span = 0;
boolean hit = false;
long increase = timeInterval * 24 * 60 * 60 * 1000;
Date time = new Date();
double mileage = 0; // 速度总数
int count = 0;// 速度计算次数
int minute = 0;
int totalColumns = (int)((toDate.getTime() + 10 - fromDate.getTime()) / (24 * 60 * 60 * 1000)); // 跨度天数
int currentCol = 1; // 当前柱子
LatLngPoint point;
System.out.println("time span=" + totalColumns+" days");
for(long t = fromDate.getTime(); t < toDate.getTime(); t = t + increase){
	span = t + increase;
	minute++;
	hit = false;
	mileage = 0;
	count = 0;
	point = null;
	for(int i = 0; i < result.size(); i++){
		Report rp = result.get(i);
		if(!hit && (t <= rp.getCreateTime().getTime())){
			hit = true;
		}
		if(hit && rp.getCreateTime().getTime() < span){
			count++;  
			if(point != null)
				mileage = mileage + Distance.distance(new LatLngPoint(rp.getSignedLat(), rp.getSignedLng()), point);
			point = new LatLngPoint(rp.getSignedLat(), rp.getSignedLng());
		}else if(hit){
			break;
		}
	}
	if(totalColumns == (minute))
		sb.append("["+minute+","+ nf.format(mileage / 1000) + "]"); // 速度为0，没有记录
	else
		sb.append("[" + minute + "," + nf.format(mileage / 1000) + "],"); // 速度为0，没有记录
	if((totalColumns < 12)			
			||((totalColumns * currentCol / 12) == (minute))
			|| (totalColumns == (minute))){ // 12个柱子之一的时间
		time.setTime(t);
		if(totalColumns == (minute))
			label.append("["+minute+",'"+localFormat.format(time)+"']");
		else
			label.append("["+minute+",'"+localFormat.format(time)+"'],");
		currentCol++;
	}
}
sb.append("]");
label.append("]");
//out.write(sb.toString());
out.println(sb.toString());
out.print(label.toString());
System.out.println("{data:" + sb.toString()+", text:"+label.toString()+"}");
com.brother.database.DataBase.closeThreadConnection();
    
    
%>